package com.kkb.service;

import com.kkb.mapper.DoctorMapper;
import com.kkb.mapper.HosregisterMapper;
import com.kkb.pojo.Doctor;
import com.kkb.pojo.DoctorExample;
import com.kkb.pojo.Hosregister;
import com.kkb.pojo.HosregisterExample;
import com.kkb.vo.QueryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class HosregisterService {
    @Resource
    private HosregisterMapper hosregisterMapper;
    @Resource
    private DoctorMapper doctorMapper;
    //查询所有挂号
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Hosregister> queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){
        HosregisterExample example = new HosregisterExample();
        HosregisterExample.Criteria criteria = example.createCriteria();
        if (vo != null){
            if (vo.getHosR_id()!=null && vo.getHosR_id()>0){
                criteria.andHosR_idEqualTo(vo.getHosR_id());
            }
            if (vo.getBeginDate()!=null){
                criteria.andHosR_timeGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate()!=null){
                criteria.andHosR_timeLessThanOrEqualTo(vo.getEndDate());
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Hosregister> list = hosregisterMapper.selectByExample(example);
        PageHelper.startPage(pageNum,pageSize);
        List<Hosregister> newList = new ArrayList<>();
        for (Hosregister hosregister : list) {
            Doctor doctor = doctorMapper.selectByPrimaryKey(hosregister.getD_id());
            hosregister.setDoctor(doctor);
            if (vo.getD_name()!=null && !"".equals(vo.getD_name().trim())
                    ||vo.getD_keshi()!=null && !"".equals(vo.getD_keshi().trim())){
                if (hosregister.getDoctor().getD_name().equals(vo.getD_name().trim())
                        ||hosregister.getDoctor().getD_keshi().equals(vo.getD_keshi().trim())){
                    newList.add(hosregister);
                }
            }
        }

        if(newList.size()>0){
            return new PageInfo<>(newList);
        }
        return new PageInfo<>(list);
    }
    //查询医生列表，用于挂号时选择医生
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Doctor> show(){
        DoctorExample example = new DoctorExample();
        return doctorMapper.selectByExample(example);
    }
    //挂号添加
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int add(Hosregister hos){
        HosregisterExample example = new HosregisterExample();
        HosregisterExample.Criteria criteria = example.createCriteria();
        if (hos.getHosR_medical()!=null && !"".equals(hos.getHosR_medical().trim())){
            criteria.andHosR_medicalEqualTo(hos.getHosR_medical().trim());
        }
        List<Hosregister> list = hosregisterMapper.selectByExample(example);
        if (list.size()>0){
            return 0;
        }
        return hosregisterMapper.insertSelective(hos);
    }
    //挂号修改
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int update(Hosregister hos){
        return hosregisterMapper.updateByPrimaryKeySelective(hos);
    }
    //退号功能
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateState(List<Integer> list){
        int count = 0;
        for (Integer id : list) {
            Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(id);
            if (hosregister.getHosR_state()!=3){
                hosregister.setHosR_state(3);
                int i = hosregisterMapper.updateByPrimaryKeySelective(hosregister);
                if (i>0){
                    count++;
                }
            }
        }
        if (count>0){
            return 1;
        }
        return 0;
    }
    //查看详细信息
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Hosregister queryById(Integer hosR_id){
        Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(hosR_id);
        Doctor doctor = doctorMapper.selectByPrimaryKey(hosregister.getD_id());
        hosregister.setDoctor(doctor);
        return hosregister;
    }
}
